//离用户最近的DIV,每次切换页面时更新
var frontDiv=null;

function $(sId){
	if(!document.getElementById){
		return false;
	}
	return document.getElementById(sId);
}

function $$(name){
	var obj=document.getElementsByName(name);
	return obj;
}

function isValid(obj){
	if (obj===null || typeof obj==="undefined" ){
		return false;
	}else{
		return true;
	}
}

/*解决$不能取出数组的问题*/
function $t(tag){
	return document.getElementsByTagName(tag);
}

var $c=function(tag){
	return document[crt](tag);
};

var $tn=function(content){
	return document.createTextNode(content);
};

var $d=function(sname,svalue){
	if(!sname){
		return false;
	}
	var a;
	if(typeof sname == 'string'){
		a=$$(sname);
	}else if(sname.length){
		a=sname;
	}else{
		a=[sname];
	}
	var isHide=svalue=='none';
	var className=!isHide?'hidden':'display';
	var toClassName=isHide?'hidden':'display';
	for(var i=0;i<a.length;i=i+1){
		var item=a[i];
		if(typeof item === 'string'){
			item=$$(item);
			$d(item,svalue);
		}else{
			if(a[i].className==className){
				a[i].className==toClassName;
			}
			if(!a[i].style||typeof svalue==='undefined'){
				continue;
			}
			a[i].style.display=svalue;
		}
	}
};

function hide(sname){
	$d(sname,'none');
}

function show(sname){
	$d(sname,'');
}

function putFront(sname){
	frontDiv=$('latestDiv');
	hide(frontDiv);
	if(typeof sname=='string'){
		sname=$(sname);
	}
	frontDiv=sname;
	show(frontDiv);
}

/*在一个textarea中显示body的HTML代码*/
function showBodyHTML(){
	var textareaName='textareaBodyHTML';
	var oTextArea=$(textareaName);
	if($(textareaName)){
		oTextArea.innerText=document.body[out];
	}else{
		oTextArea=document[crt]('textarea');
		oTextArea.setAttribute('name',textareaName);
		oTextArea.setAttribute('id',textareaName);
		oTextArea.setAttribute('rows','10');
		oTextArea.setAttribute('cols','30');
		oTextArea.innerText=document.body[out];
		document.body.appendChild(oTextArea);
	}
}

//特殊校验的提示语言封装到这里，方便以后统一修改。
var promptNotNull=function(info){
	alert("["+info+"]不能为空，请重新输入！");
};

//非空校验:一个控件
var checkItem=function(item){
	var nullable=item.isnullable;
	if(nullable){
		nullable=false;
	}else{
		nullable=true;
	}
	var empty=item.value;
	if(!nullable&&!empty){
		promptNotNull(item.prompt);
		item.focus();
		return false;
	}
	return true;
};

var isHidden=function(item){
	while(item){
		item=item.parentElement;
		if(item.style.display=='none'){
			return true;
		}
	}
	return false;
};

var reg_email=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;

var isEmail=function(str_email){
	var isEmail=reg_email.test(str_email);
	if(!isEmail){
		//alert('Email格式不正确！')
		return false;
	}
	return true;
}

/*xml核心函数*/
var XMLDocument=function(xmlString,filePath){
	this.xmlString=xmlString;
	this.filePath=filePath;
	this.xmlDocument=this.getEmptyDocument();
};

XMLDocument.prototype={
	getEmptyDocument:function(){
		var doc=null;
		if(window.ActiveXObject){
	  		try{
				doc=new ActiveXObject("MSXML2.DOMDocument");
			}catch(e){
				try{
					doc=new ActiveXObject("Microsoft.XMLDOM");
				}catch(e){
					alert(e.message);
				}
			}
	  	}else if (document.implementation && document.implementation.createDocument) {
		   	doc = document.implementation.createDocument("", "", null);
	  	}
	  	doc.async=false;
	  	return doc;
	},
	//通过字符创建一个DOM文档的对象
	getDocumentFromString:function(xmlString){
		if(!xmlString){
			xmlString=this.xmlString;
		}
		var doc=this.getEmptyDocument();
		if(!xmlString){
			xmlString="<?xml version=\"1.0\"?>"
		}
		try{
			doc.loadXML(xmlString);
		}catch(e){
			alert(e.message);
		}
		return doc;
	},
	//通过文件路径创建一个DOM文档的对象
	getDocumentFromFile:function(filePath){
		if(!filePath){
			filePath=this.filePath;
		}
		var doc=this.getEmptyDocument();
		try{
			doc.load(filePath);
		}catch(e){
			alert(e.message);
		}
		return doc;
	},
	addRoot:function(rootName){
		var rootElement=this.xmlDocument.createElement(rootName);
		this.xmlDocument.documentElement=rootElement;
		return rootElement;
	},
	getRoot:function(rootName){
		return this.xmlDocument.documentElement;
	},
	getNewTag:function(tagName){
		return this.xmlDocument.createElement(tagName);
	}
};

function addNumberFields(fieldsArray){
	for(var i=0;i<fieldsArray.length;i=i+1){
		var field=fieldsArray[i];
		if(typeof field=='string'){
			field=$(field);
		}
		field.attachEvent('onkeydown',checkOnlyNumber);
	}
}

function checkOnlyNumber(){
	if (![8,13,45,46,48,49,50,51,52,53,54,55,56,57].has(window.event.keyCode)){
		return false;
	}
}

function enterToTab(){
  if(event.srcElement.type != 'button' 
  && event.srcElement.type != 'textarea' && event.keyCode == 13){
    event.keyCode = 9;
  }
}

function enterJump(){
	var kcode=event.keyCode;
	
	if(kcode == 13){
		event.keyCode = 9;
	}
	return true;
}

//需要重构
var curObjTr = null;
var oldObjTr_c = "";
function tr_onclick(objTr,bgcolor) {
	if(curObjTr != null){
		curObjTr.style.background = oldObjTr_c;
	}
	curObjTr = objTr;
	oldObjTr_c = bgcolor;
	curObjTr.style.background = '#FDF6BA';
}

function checkNotNull(aIds){
	return checkNotNullByStar();
}

function checkNotNullByStar(){
	var textBoxes=document.getElementsByTagName('input');
	var iTextBoxes=textBoxes.length;
	for(var i=0;i<iTextBoxes;i=i+1){
		var textBox=textBoxes[i];
		if(textBox.parentNode&&textBox.parentNode.id=='stay')continue;
		if(textBox.type!='text')continue;
		if(textBox.disabled)continue;
		if(textBox.readOnly)continue;
		if(textBox.type!='text')continue;
		if(!isDisplay(textBox,true))continue;
		if(textBox.value!='')continue;
		if(!textBox.parentNode.previousSibling[out].match(/\*{1}/)){
			continue;
		}
		promptNotNull(textBox.parentNode.previousSibling.firstChild.data);
	    textBox.focus();
	    return false;
	}
	return true;
}

function checkNotNullByIds(aIds){
	var iIds=aIds.length;
	for(var i=0;i<iIds;i=i+1){
		var oId=$(aIds[i]);
		if(isDisplay(oId,true)&&oId.value==''){
			promptNotNull(oId.parentNode.previousSibling.firstChild.data);
		    oId.focus();
		    return false;
		}
	}
	return true;
}

function isDisplay(element,deep){
	var dom = typeof element == "string" ?$(element) : element;
    if(!dom||!dom.style){
        return null;
    }
    var vis = !(dom.style.visibility == "hidden" || dom.style.display == "none");
    if(deep !== true || !vis){
        return vis;
    }
    var p = dom.parentNode;
    while(p && p.tagName.toLowerCase() != "body"){
        if(!isDisplay(p)){
            return false;
        }
        p = p.parentNode;
    }
    return true;
}

function isUrl(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

function isUrlTest(){
	var s='http:\\dls\\sdf';
	alert(isUrl(s));
}

function produceDetailInfo(list,itemMap){
	var containerDiv=document.createElement('div');
	containerDiv.className='detailContainer';
	for(key in itemMap){
		var value=list[key];
		if(!value){
			continue;
		}
		var labelName=itemMap[key];
		var oDiv=document.createElement('div');
		oDiv.className='infomationItem';
		var labelSpan=document.createElement('span');
		labelSpan.className='detailLabel';
		labelSpan.appendChild(document.createTextNode(labelName+': '));
		oDiv.appendChild(labelSpan);
		var contentSpan=document.createElement('span');
		contentSpan.innerHTML=value;
		contentSpan.className='detailContent';
		oDiv.appendChild(contentSpan);
		containerDiv.appendChild(oDiv);
	}
	return containerDiv;
}

function transformXML(receivedDocument){
	var jsonObject=[];
	var lists=receivedDocument.getElementsByTagName('list');
	for(var i=0;i<lists.length;i=i+1){
		var nodeMap=getList(lists[i]);
		jsonObject.push(nodeMap);
	}
	return jsonObject;
}
	
function getList(list){
	var nodes=list.getElementsByTagName('item');
	var nodeMap={};
	for(var i=0;i<nodes.length;i=i+1){
		var node=nodes[i];
		var nameAttribute=node.getAttribute('name');
		var nodeValue=node.firstChild?node.firstChild.nodeValue:'';
		if(!nameAttribute){
			continue;
		}
		nodeMap[nameAttribute]=nodeValue;
	}
	return nodeMap;
}

function showPopupDiv(iLeft,iTop,iWidth,iHeight,sHtml){
	var outterDiv = document.createElement("div");
	outterDiv.className="div_one";
	outterDiv.style.left=iLeft;
	outterDiv.style.width=iWidth;
	outterDiv.style.top=IeTrueBody().scrollTop + iTop;
	outterDiv.style.height=iHeight;
	var html="<div onMouseDown='handleMouseDown(event,this)' class='dragBar' style='width:190px' name='dragDIV'><div class='closeArea' style='margin-right:1px' onclick='closeit(this);'><img src='../drag/closeMod.gif\'></div></div>";
	html+=sHtml;
	outterDiv.innerHTML = html;
	document.body.appendChild(outterDiv);
	return outterDiv;
}
